import Util from '../../Util';
const utils = {
    /**
     * 随机生成字母串
     * @param {string} [length=4]  字母长度
     */
    getRandomWords(length = 4) {
        const result = [];
        for (let i = 0; i < length; i++) {
            const ranNum = Math.ceil(Math.random() * 25); // 生成一个0到25的数字
            // 大写字母'A'的ASCII是65,A~Z的ASCII码就是65 + 0~25;然后调用String.fromCharCode()传入ASCII值返回相应的字符并push进数组里
            result.push(String.fromCharCode(65 + ranNum));
        }
        return result.join('');
    },
    /**
     * 获取文本字符串默认fontsize下的宽和高
     * @param {string} str 文本字符串
     */
    getTextSize(str: string, fontSize: number) {
        let width = 0;
        let height = 0;
        const html = document.createElement('span');
        html.innerText = str;
        const className = Utils.getRandomWords();
        html.className = className;
        if (fontSize) {
            html.style.fontSize = fontSize + 'px';
        }
        const bodyDom = document.querySelector('body');
        if (bodyDom) {
            bodyDom.appendChild(html);
        }
        const textDom = document.querySelector('.' + className) as any;
        if (textDom) {
            width = textDom.offsetWidth;
            height = textDom.offsetHeight;
            textDom.remove();
        }
        return { width, height };
    },
    getStyle(opts: any = { width: 22, height: 22 }, ...numbers: any[]) {
        // let total = 0;
        let maxWidth = 0;
        let totalHeight = 0;
        numbers.forEach((num) => {
            // total += num * 1;
            const { width, height } = Utils.getTextSize(num + '人');
            if (width > maxWidth) {
                maxWidth = width
            }
            totalHeight += height + 1;
        });
        // const { width } = Utils.getTextSize(numbers.join(','));
        // const size = Math.ceil(width / 2 + 5);
        const size = (maxWidth > totalHeight ? maxWidth : totalHeight) + 40;
        const originSize = opts.width > opts.height ? opts.width : opts.height;
        const scale = size / originSize;
        const style = Object.assign({
            offsetX: size / 2,
            offsetY: size / 2,
            opacity: 1,
            rotation: 0,
            scale
        }, opts)
        return style;
    },
    getLevelByZoom(zoom: any, zoomLevelMap: any) {
        const key = Object.keys(zoomLevelMap).find((z: any) => zoom * 1 <= z * 1)
        if (key) {
            return zoomLevelMap[key];
        }
        return 0;
    },
    getZoomByLevel(level: any, zoomLevelMap: any) {
        const zoom = Object.keys(zoomLevelMap).find((item: any) => zoomLevelMap[item] === level * 1);
        return zoom || 8;
    },
    /**
     * 创建图片符号
     * @param scale
     * @param picSource
     * @param totalnum
     * @param totalsuspectednum
     * @returns {Promise}
     */
    createText(scale: number, picSource: string, totalnum: number, totalsuspectednum: number) {
        const canvas = document.createElement('canvas');
        const ctx = canvas.getContext('2d');

        return new Promise((resolve, reject) => {
            const image2 = new Image();
            image2.onload = function() {
                canvas.width = image2.width * scale;
                canvas.height = image2.height * scale;
                if(!!ctx) {
                    ctx.drawImage(image2, 0, 0, canvas.width, canvas.height);
                    createNumberPeople(ctx, totalnum, "人", 38, 40, "#e9392b");
                    createNumberPeople(ctx, totalsuspectednum, "人", 38, 56, "#f49530");
                    resolve(canvas.toDataURL("image/png"));
                } else {
                    resolve(null);
                }
            }
            image2.onerror = function() {
                reject(new Error('Could not load image!'))
            }
            image2.src = picSource;
        })

        function createNumberPeople(ctx: any, num: number, peopleText: string,
                                    x: number, y: number, fillStyle: string) {
            createNumberText(ctx, num, x, y, fillStyle);
            const numberWidth = ctx.measureText(num).width;
            createPeopleText(ctx, peopleText, x + numberWidth, y);
        }

        function createNumberText(ctx: any, num: number, x: number, y: number, fillStyle: string) {
            // 设置字体
            ctx.font = "14px bold";
            // 设置颜色
            ctx.fillStyle = fillStyle;
            // 设置水平对齐方式
            ctx.textAlign = "center";
            // 设置垂直对齐方式
            ctx.textBaseline = "middle";
            // 绘制文字（参数：要写的字，x坐标，y坐标）
            ctx.fillText(num, x, y);
        }

        function createPeopleText(ctx: any, peopleText: string, x: any, y: any) {
            // 设置颜色
            ctx.font = "16px";
            ctx.fillStyle = "white";
            ctx.fillText(peopleText, x, y);
        }
    }
}
export const Utils = Object.assign({}, G.utils.CommonUtil, utils, Util);
